home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / Draw / Sources / ClipCmds.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  12.4 KB  |  427 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                ClipCmds.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef CLIPCMDS_H
  15. #include "ClipCmds.h"
  16. #endif
  17.  
  18. #ifndef DRAWCONT_H
  19. #include "DrawCont.h"
  20. #endif
  21.  
  22. #ifndef DRAWSEL_H
  23. #include "DrawSel.h"
  24. #endif
  25.  
  26. #ifndef BASESHP_H
  27. #include "BaseShp.h"
  28. #endif
  29.  
  30. #ifndef BOUNDSHP_H
  31. #include "BoundShp.h"
  32. #endif
  33.  
  34. #ifndef LINESHP_H
  35. #include "LineShp.h"
  36. #endif
  37.  
  38. #ifndef OVALSHP_H
  39. #include "OvalShp.h"
  40. #endif
  41.  
  42. #ifndef RECTSHP_H
  43. #include "RectShp.h"
  44. #endif
  45.  
  46. #ifndef RRECTSHP_H
  47. #include "RRectShp.h"
  48. #endif
  49.  
  50. #ifndef TEXTSHP_H
  51. #include "TextShp.h"
  52. #endif
  53.  
  54. #ifndef DRAWPART_H
  55. #include "DrawPart.h"
  56. #endif
  57.  
  58. #ifndef DRAWFRM_H
  59. #include "DrawFrm.h"
  60. #endif
  61.  
  62. #ifndef DRAWLINK_H
  63. #include "DrawLink.h"
  64. #endif
  65.  
  66. #ifndef DEFINES_K
  67. #include "Defines.k"
  68. #endif
  69.  
  70. // ----- FrameWork Includes -----
  71.  
  72. #ifndef FWPRESEN_H
  73. #include "FWPresen.h"
  74. #endif
  75.  
  76. #ifndef FWSESION_H
  77. #include "FWSesion.h"
  78. #endif
  79.  
  80. // ----- OS Includes -----
  81.  
  82. #ifndef FWORDCOL_H
  83. #include "FWOrdCol.h"
  84. #endif
  85.  
  86. // ----- OpenDoc Includes -----
  87.  
  88. #ifndef SOM_Module_OpenDoc_Commands_defined
  89. #include <CmdDefs.xh>
  90. #endif
  91.  
  92. //========================================================================================
  93. // RunTime Info
  94. //========================================================================================
  95.  
  96. #ifdef FW_BUILD_MAC
  97. #pragma segment odfdrawcommand
  98. #endif
  99.  
  100. FW_DEFINE_AUTO(CDrawClipboardCommand)
  101.  
  102. //========================================================================================
  103. // CDrawClipboardCommand
  104. //========================================================================================
  105.  
  106. //----------------------------------------------------------------------------------------
  107. //    CDrawClipboardCommand constructor
  108. //----------------------------------------------------------------------------------------
  109.  
  110. CDrawClipboardCommand::CDrawClipboardCommand(Environment* ev, 
  111.                                             ODCommandID commandID,
  112.                                             CDrawPart* part, 
  113.                                             CDrawFrame* frame, 
  114.                                             CDrawSelection* selection,
  115.                                             FW_Boolean canUndo) :
  116.     FW_CClipboardCommand(ev, commandID, frame, canUndo),
  117.         fDrawPart(part),
  118.         fDrawSelection(selection),
  119.         fUndoContent(NULL),
  120.         fSavedLink(NULL),
  121.         fLinkSources(NULL)
  122. {
  123.     FW_END_CONSTRUCTOR
  124. }
  125.  
  126. //----------------------------------------------------------------------------------------
  127. //    CDrawClipboardCommand destructor
  128. //----------------------------------------------------------------------------------------
  129.  
  130. CDrawClipboardCommand::~CDrawClipboardCommand()
  131. {
  132.     FW_START_DESTRUCTOR
  133.     delete fUndoContent;
  134.     delete fLinkSources;
  135. }
  136.  
  137. //----------------------------------------------------------------------------------------
  138. //    CDrawClipboardCommand::SaveUndoState
  139. //----------------------------------------------------------------------------------------
  140. void CDrawClipboardCommand::SaveUndoState(Environment* ev)    // Override
  141. {
  142.     ODCommandID commandID = GetCommandID(ev);
  143.     
  144.     if (commandID == kODCommandCut || commandID == kODCommandClear)
  145.     {
  146.         FW_ASSERT(fUndoContent == NULL);
  147.         fUndoContent = FW_NEW(CDrawUndoContent, (ev, fDrawPart, fDrawSelection));
  148.         
  149.         // Obtain a list of links sources to update before removing anything!
  150.         // This will do for now, but doesn't filter out any sources
  151.         // which will be removed in their entirety, so needn't be updated.
  152.         
  153.         fLinkSources = fDrawSelection->GetSelectedLinkSources();
  154.         
  155.     }
  156. }
  157.  
  158. //----------------------------------------------------------------------------------------
  159. //    CDrawClipboardCommand::SaveRedoState
  160. //----------------------------------------------------------------------------------------
  161. void CDrawClipboardCommand::SaveRedoState(Environment* ev)    // Override
  162. {
  163.     if (this->IsPasteWithoutLink(ev))
  164.     {
  165.         FW_ASSERT(fUndoContent == NULL);
  166.         fUndoContent = FW_NEW(CDrawUndoContent, (ev, fDrawPart, fDrawSelection));
  167.     }    
  168. }
  169.  
  170. //----------------------------------------------------------------------------------------
  171. //    CDrawClipboardCommand::FreeUndoState
  172. //----------------------------------------------------------------------------------------
  173. void CDrawClipboardCommand::FreeUndoState(Environment* ev)    // Override
  174. {
  175.     ODCommandID commandID = GetCommandID(ev);
  176.     
  177.     if (commandID == kODCommandCut || commandID == kODCommandClear)
  178.     {
  179.         // Delete link sources left empty because all their shapes were removed
  180.         fUndoContent->DeleteEmptyLinkSources(ev);
  181.  
  182.         // Delete undo data
  183.         fUndoContent->DeleteSavedLinks(ev);
  184.     }
  185.     else if (this->IsPasteWithoutLink(ev))
  186.     {
  187.         fUndoContent->CommitPasteDone(ev);
  188.     }
  189. }
  190.  
  191. //----------------------------------------------------------------------------------------
  192. //    CDrawClipboardCommand::FreeRedoState
  193. //----------------------------------------------------------------------------------------
  194.  
  195.  
  196. //----------------------------------------------------------------------------------------
  197. //    CDrawClipboardCommand::UndoIt
  198. //----------------------------------------------------------------------------------------
  199. void CDrawClipboardCommand::UndoIt(Environment* ev)    // Override
  200. {
  201.     FW_CClipboardCommand::UndoIt(ev);    // call inherited
  202.  
  203.     switch (GetCommandID(ev))
  204.     {
  205.         case kODCommandCut:
  206.         case kODCommandClear:
  207.             fUndoContent->RestoreShapeSelection(ev);
  208.             fUndoContent->RestoreLinks(ev);
  209.             break;
  210.  
  211.         case kODCommandPasteAs:
  212.             if (fSavedLink)
  213.             {
  214.                 fSavedLink->SelectShapes(ev);    // set the selection's update shape
  215.                 GetDrawLinkManager(ev)->UndoPasteAs(ev, fSavedLink);
  216.                 GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  217.                 break;
  218.             }    
  219.         //    else fall through to kODCommandPaste
  220.  
  221.         case kODCommandPaste:
  222.             fUndoContent->RemoveShapeSelection(ev);
  223.             break;
  224.     }    
  225. }
  226.  
  227. //----------------------------------------------------------------------------------------
  228. //    CDrawClipboardCommand::RedoIt
  229. //----------------------------------------------------------------------------------------
  230. void CDrawClipboardCommand::RedoIt(Environment* ev)    // Override
  231. {
  232.     FW_CClipboardCommand::RedoIt(ev);    // call inherited
  233.  
  234.     switch (GetCommandID(ev))
  235.     {
  236.         case kODCommandCut:
  237.         case kODCommandClear:
  238.             fUndoContent->RemoveShapeSelection(ev);
  239.             fUndoContent->RemoveFromLinks(ev);
  240.             break;
  241.  
  242.         case kODCommandPasteAs:
  243.             if (fSavedLink)
  244.             {
  245.                 GetDrawLinkManager(ev)->RedoPasteAs(ev, fSavedLink);
  246.                 GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  247.                 break;
  248.             }    
  249.         //    else fall through to kODCommandPaste
  250.  
  251.         case kODCommandPaste:
  252.             fUndoContent->RestoreShapeSelection(ev);
  253.             break;
  254.     }    
  255. }
  256.  
  257. //----------------------------------------------------------------------------------------
  258. //    CDrawClipboardCommand::PreCommand
  259. //----------------------------------------------------------------------------------------
  260.  
  261. void CDrawClipboardCommand::PreCommand(Environment* ev)
  262. {
  263.     ODCommandID commandID = GetCommandID(ev);
  264.     
  265.     if (commandID == kODCommandPaste || commandID == kODCommandPasteAs)
  266.         fDrawSelection->CloseSelection(ev);
  267. }
  268.  
  269. //----------------------------------------------------------------------------------------
  270. //    CDrawClipboardCommand::CommandDone
  271. //----------------------------------------------------------------------------------------
  272.  
  273. void CDrawClipboardCommand::CommandDone(Environment* ev)
  274. {
  275.     ODCommandID commandID = GetCommandID(ev);
  276.  
  277.     if (commandID == kODCommandPaste)
  278.         fDrawSelection->CenterSelection(ev, GetContextFrame(ev));
  279.     else if (commandID == kODCommandPasteAs)
  280.     {
  281.         fSavedLink = (CDrawSubscribeLink*)GetNewLink(ev);
  282.         if (fSavedLink && fSavedLink->IsEstablished(ev))    // cross-doc link not yet established
  283.         {
  284.             // Select the newly-subscribed shapes
  285.             fSavedLink->SelectShapes(ev);
  286.         
  287.             // Offset the selection, saving the offset for later updates
  288.             FW_CPoint offset = fDrawSelection->CenterSelection(ev, GetContextFrame(ev));
  289.         
  290.             // Save information about newly-created link
  291.             fSavedLink->SetUpdateOffset(ev, offset);
  292.         }
  293.         else
  294.             fDrawSelection->CenterSelection(ev, GetContextFrame(ev));
  295.  
  296.     }
  297.     else if (commandID == kODCommandCut || commandID == kODCommandClear)
  298.     {
  299.         fUndoContent->RemoveFromLinks(ev);
  300.     }
  301. }
  302.  
  303. //----------------------------------------------------------------------------------------
  304. //    CDrawClipboardCommand::IsPasteWithoutLink
  305. //----------------------------------------------------------------------------------------
  306.  
  307. FW_Boolean CDrawClipboardCommand::IsPasteWithoutLink(Environment* ev) const
  308. {
  309.     if (GetCommandID(ev) == kODCommandPaste)
  310.         return TRUE;
  311.     if (GetCommandID(ev) == kODCommandPasteAs)
  312.         return (fSavedLink == NULL);
  313.         
  314.     return FALSE;
  315. }
  316.  
  317. //----------------------------------------------------------------------------------------
  318. //    CDrawClipboardCommand::GetDrawLinkManager
  319. //----------------------------------------------------------------------------------------
  320.  
  321. CDrawLinkManager* CDrawClipboardCommand::GetDrawLinkManager(Environment* ev) const
  322. {
  323.     return (CDrawLinkManager*)GetPart(ev)->GetLinkManager(ev);
  324. }
  325.  
  326. //----------------------------------------------------------------------------------------
  327. //    CDrawClipboardCommand::CommitUndone
  328. //----------------------------------------------------------------------------------------
  329.  
  330.  
  331.  
  332. //----------------------------------------------------------------------------------------
  333. //    CDrawClipboardCommand::CommitDone
  334. //----------------------------------------------------------------------------------------
  335.  
  336.  
  337. //----------------------------------------------------------------------------------------
  338. //    CDrawClipboardCommand::PropagateChanges
  339. //----------------------------------------------------------------------------------------
  340. void CDrawClipboardCommand::PropagateChanges(Environment* ev,  ODUpdateID id )
  341. {
  342.     if (fLinkSources)
  343.     {
  344.         //--- Update affected links ---
  345.         id = FW_CSession::UniqueUpdateID(ev);
  346.         CDrawPublishLinkCollectionIterator iter(fLinkSources);
  347.         for (CDrawPublishLink* link = iter.First(); iter.IsNotComplete(); link = iter.Next())
  348.         {
  349.             link->ContentUpdated(ev, id);
  350.         }
  351.     }
  352.     
  353.     FW_CClipboardCommand::PropagateChanges(ev, id);
  354. }
  355.  
  356. //========================================================================================
  357. // CDuplicateCommand
  358. //========================================================================================
  359.  
  360. FW_DEFINE_AUTO(CDuplicateCommand)
  361.  
  362. //----------------------------------------------------------------------------------------
  363. //    CDuplicateCommand constructor
  364. //----------------------------------------------------------------------------------------
  365.  
  366. CDuplicateCommand::CDuplicateCommand(Environment* ev, CDrawFrame* frame) 
  367. :    FW_CCommand(ev, cDuplicate, frame, FW_kCanUndo),
  368.     fDrawFrame(frame),
  369.     fBeganTransaction(false)
  370. {
  371.     SetActionType(ev, kODEndAction);
  372.     SetMenuStringsFromResource(ev, kDrawUndoStrings, kUndoDuplicateMsg, kRedoDuplicateMsg);
  373.     FW_END_CONSTRUCTOR
  374. }
  375.  
  376. //----------------------------------------------------------------------------------------
  377. //    CDuplicateCommand destructor
  378. //----------------------------------------------------------------------------------------
  379.  
  380. CDuplicateCommand::~CDuplicateCommand()
  381. {
  382.     FW_START_DESTRUCTOR
  383. }
  384.  
  385. //----------------------------------------------------------------------------------------
  386. //    CDuplicateCommand::DoIt
  387. //----------------------------------------------------------------------------------------
  388. void CDuplicateCommand::DoIt(Environment* ev)
  389. {
  390.     //--- Create a Copy command
  391.     FW_CClipboardCommand* copyCmd = fDrawFrame->NewClipboardCommand(ev, kODCommandCopy);
  392.     if (copyCmd == NULL)
  393.         FW_Failure(kODErrOutOfMemory);
  394.  
  395.     //--- Execute the Copy command
  396.     copyCmd->Execute(ev);
  397.  
  398.     //--- Post an empty Begin action
  399.     AddAction(ev, kODBeginAction, NULL, 0, GetUndoString(ev), GetRedoString(ev));
  400.     fBeganTransaction = true;
  401.  
  402.     //--- Create and execute a Paste command
  403.     FW_CClipboardCommand* pasteCmd = fDrawFrame->NewClipboardCommand(ev, kODCommandPaste);
  404.     if (pasteCmd == NULL)
  405.         FW_Failure(kODErrOutOfMemory);
  406.  
  407.     if (!pasteCmd->Execute(ev))
  408.         FW_Failure(kODErrUndefined);    // better error code? kODErrUndefined?
  409.  
  410.     // Execute will post an End action for this command
  411. }
  412.  
  413. //----------------------------------------------------------------------------------------
  414. //    CDuplicateCommand::FreeUndoStateAfterFailure
  415. //----------------------------------------------------------------------------------------
  416.  
  417. void CDuplicateCommand::FreeUndoStateAfterFailure(Environment* ev)
  418. {
  419.     // Called when a failure occurred in CDuplicateCommand::DoIt.
  420.     if (fBeganTransaction)
  421.     {
  422.         // Abort transaction
  423.         FW_CSession::GetUndo(ev)->AbortCurrentTransaction(ev);
  424.     }
  425. }
  426.  
  427.